package socket;

import java.io.*;
import java.net.Socket;

/**
 * 服务器线程处理类
 * Created by zzy on 2018/1/9.
 */
public class ServerThread extends Thread {
    //和本线程相关的socket
    Socket socket=null;

    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    //线程执行操作，相应客户端的星球


    @Override
    public void run() {
        //3、获取输入流，用来读取客户端发送的登陆信息
        InputStream inputStream = null;//字节输入流
        try {
            inputStream = socket.getInputStream();
        InputStreamReader inputStreamReader=new InputStreamReader(inputStream,"UTF-8");
        BufferedReader reader=new BufferedReader(inputStreamReader);
        String data=reader.readLine();
        while (data!=null){
            Print.print("我是服务器。客户端说:"+data);
            data=reader.readLine();
        }
        socket.shutdownInput();
        //4、相应客户端
        OutputStream outputStream= socket.getOutputStream();
        PrintWriter pw=new PrintWriter(outputStream);//将输出流包装为打印流
        pw.write("欢迎您"+data);
        pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
